<!--
 * @Description: 精英粉丝 修改自源文件student-peifans-old
 * @Author: liyujie
 * @Date: 2021/06/04
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/06/04
-->
<template>
    <div class="wrapper">
        <div class="content-wrapper">
            <!-- header -->
            <div class="header">
                <div class="header-title">
                    精英<span style="color: #2FBDB3">({{ user.true_name }}-{{ user.phone }})</span>的粉丝
                </div>
            </div>
            <!-- /header -->
            <!-- 筛选-->
            <div class="filter-view">
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        姓名
                    </p>
                    <el-input
                        placeholder="搜索用户姓名"
                        v-model="filterData.fans_keyword"
                        suffix-icon="el-icon-search"
                        clearable
                        size="medium"
                        style="width: 240px"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        用户编号
                    </p>
                    <el-input
                        placeholder="搜索用户编号"
                        v-model="filterData.keyword_student_id"
                        suffix-icon="el-icon-search"
                        clearable
                        size="medium"
                        style="width: 240px"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        是否购买
                    </p>
                    <el-select
                        v-model="filterData.is_buy"
                        placeholder="全部"
                        size="medium"
                        @change="getTableData(1)"
                    >
                        <el-option
                            label="全部"
                            :value="0"
                        />
                        <el-option
                            label="是"
                            :value="1"
                        />
                        <el-option
                            label="否"
                            :value="-1"
                        />
                    </el-select>
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        是否班级认证
                    </p>
                    <el-select
                        v-model="filterData.class_verify"
                        placeholder="全部"
                        size="medium"
                        @change="getTableData(1)"
                    >
                        <el-option
                            label="全部"
                            :value="99"
                        />
                        <el-option
                            label="是"
                            :value="1"
                        />
                        <el-option
                            label="否"
                            :value="0"
                        />
                    </el-select>
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        精英等级
                    </p>
                    <el-select
                        v-model="filterData.agent_level"
                        filterable
                        clearable
                        placeholder="全部"
                        size="medium"
                        @change="getTableData(1)"
                    >
                        <el-option
                            label="全部"
                            :value="99"
                        />
                        <el-option
                            v-for="item in 6"
                            :key="item"
                            :label="`E${item - 1}`"
                            :value="item - 1"
                        />

                    </el-select>
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        学校名称
                    </p>
                    <el-cascader
                        placeholder="请选择"
                        :options="school_tree"
                        filterable
                        clearable
                        change-on-select
                        expand-trigger="click"
                        v-model="school"
                        size="medium"
                        :props="{
                            label: 'name',
                            value: 'id',
                            children: 'children'
                        }"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        班级名称
                    </p>
                    <el-input
                        placeholder="搜索班级名称"
                        v-model="filterData.class_name"
                        suffix-icon="el-icon-search"
                        clearable
                        size="medium"
                        style="width: 240px"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="getTableData(1)"
                    >
                        查询
                    </el-button>
                </div>
            </div>
            <!--筛选-->
            <!-- table -->
            <div class="table-wrapper">
                <el-table
                    :data="tableData"
                    height="100%"
                    style="width: 100%;"
                    ref="table"
                    :cell-style="{height: '105px'}"
                    class="data-table"
                    v-loading="tableLoading"
                    row-key="student_no"
                    @selection-change="handleSelectedChange"
                >
                    <el-table-column
                        align="center"
                        width="50"
                        type="selection"
                        fixed="left"
                    />
                    <el-table-column
                        label="用户信息"
                        width="220"
                        fixed="left"
                        :formatter="emptyFormatter"
                    >
                        <template slot-scope="scope">
                            <div class="cell-title">
                                <div class="user-name">{{ scope.row.true_name ? scope.row.true_name : '--' }}</div>
                                <div class="user-tags">
                                    <div class="tag-item">
                                        <img
                                            src="@/assets/image/finance/class-light.png"
                                            v-if="scope.row.class_verify"
                                        >
                                        <img
                                            src="@/assets/image/finance/class.png"
                                            v-else
                                        >
                                    </div>
                                </div>
                            </div>
                            <div class="cell-tips">手机号：{{ scope.row.phone }}</div>
                            <div class="cell-tips">用户ID：{{ scope.row.student_no }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="精英等级"
                        align="center"
                        :formatter="emptyFormatter"
                    >
                        <template slot-scope="scope">
                            {{ scope.row.agent_level !== -1 ? `E${scope.row.agent_level}` : '--' }}
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="学校名称"
                        prop="school_name"
                        show-overflow-tooltip
                        width="130"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        label="学院名称"
                        prop="school_college_name"
                        show-overflow-tooltip
                        width="130"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        label="班级名称"
                        prop="class_name"
                        show-overflow-tooltip
                        width="130"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        prop="fans_num"
                        label="二级粉丝"
                        align="center"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                    >
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                v-if="scope.row.fans_num"
                                @click="fnJumpFans(scope.row)"
                            >
                                {{ scope.row.fans_num }}人
                            </el-button>
                            <div v-else>--</div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="join_time"
                        label="成为粉丝时间"
                        :width="140"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                    >
                        <template slot-scope="scope">
                            {{ scope.row.join_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="is_buy"
                        label="是否购买"
                        align="center"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                    >
                        <template slot-scope="scope">
                            <span v-if="scope.row.is_buy === 1">是</span>
                            <span v-if="scope.row.is_buy === 0">否</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="buy_num"
                        align="center"
                        label="购买次数"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        label="购买记录"
                        align="center"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                    >
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                :disabled="scope.row.buy_num == 0"
                                @click="fnGetbuyList(scope.row, 1)"
                            >
                                查看
                            </el-button>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="person_money"
                        label="个人消费金额"
                        align="center"
                        show-overflow-tooltip
                        :width="140"
                        :formatter="emptyFormatter"
                    >
                        <template slot-scope="scope">
                            <span>{{ scope.row.person_money | setMoney }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="rebate"
                        align="center"
                        label="预估佣金"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                    >
                        <template slot-scope="scope">
                            <span>{{ scope.row.rebate | setMoney }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column
                        prop="rebate_money"
                        label="所有收入总计"
                        :width="140"
                        align="center"
                        show-overflow-tooltip
                        :formatter="emptyFormatter"
                    >
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                v-if="scope.row.total_income"
                                @click="handleRoute('studentDataMoney', {agent_no: scope.row.agent_no,true_name: scope.row.true_name, phone: scope.row.phone})"
                            >
                                {{ scope.row.total_income | setMoney }}
                            </el-button>
                            <div v-else>--</div>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <!-- /table -->
            <!-- /分页 -->
            <div class="pagination-wrapper clearfix">
                <div class="op-view">
                    <el-checkbox
                        label="当页全选"
                        v-model="checkAllStatus"
                        :indeterminate="checkIndeterminate"
                        :disabled="tableData.length === 0"
                        @change="handleSelectedAll"
                    />
                    <el-button
                        type="text"
                        size="medium"
                        style="margin-left: 12px"
                        :disabled="multipleSelection.length === 0"
                        @click="handleDelete()"
                    >
                        删除
                    </el-button>

                    <el-pagination
                        layout="total, prev, pager, next, jumper"
                        background
                        :current-page.sync="page.now_page"
                        :total="page.total_count"
                        :page-size="page.page_size"
                        @current-change="getTableData"
                    />
                </div>
            </div>
        </div>
        <!-- 查看购买记录 -->
        <el-dialog
            :visible.sync="DialogVisibleList"
            width="760px"
            class="seckillDialogVisible"
            title="购买记录"
        >
            <el-table
                :data="tableDataReord"
                max-height="500px"
                style="width: 100%;"
                border
            >
                <el-table-column
                    label="序号"
                    width="50"
                    align="center"
                    type="index"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    prop="create_time"
                    label="下单日期"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        {{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                    </template>
                </el-table-column>
                <el-table-column
                    prop="aim_type"
                    label="商品类型"
                    show-overflow-tooltip
                >
                    <template slot-scope="scope">
                        {{ scope.row.aim_type | setAimtype }}
                    </template>
                </el-table-column>
                <el-table-column
                    prop="ori_money"
                    label="实付款"
                    show-overflow-tooltip
                >
                    <template slot-scope="scope">
                        {{ scope.row.ori_money }}
                    </template>
                </el-table-column>
                <el-table-column
                    prop="rebate_money"
                    label="产生佣金"
                    show-overflow-tooltip
                >
                    <template slot-scope="scope">
                        {{ scope.row.rebate_money | setMoney }}
                    </template>
                </el-table-column>
                <el-table-column
                    prop="status"
                    label="订单状态"
                    show-overflow-tooltip
                >
                    <template slot-scope="scope">
                        {{ scope.row.status | setStatus }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    show-overflow-tooltip
                >
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            @click="fnJumpShop(scope.row)"
                        >
                            查看
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div style="padding: 15px 0">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page.sync="pageOrder.now_page"
                    :total="pageOrder.total_count"
                    :page-size="pageOrder.page_size"
                    @current-change="fnGetbuyList"
                    style="float: right"
                />
            </div>
        </el-dialog>
        <!-- /查看购买记录 -->
    </div>
</template>

<script>
import {back} from '@/mixin/back';
import RejectDialog from "@/components/dialog/reject-dialog";

export default {
    name: 'StudentPeifans',
    mixins: [back],
    components: {RejectDialog},
    data() {
        return {
            user: {
                true_name: '',
                phone: ''
            },
            filterData: {
                fans_keyword: '',
                // 学生编号
                keyword_student_id: '',
                is_buy: 0,
                agent_no: '',
                // 代理等级
                agent_level: 99,
                school_id: '',
                // 是否为校园精英
                is_agent: 99,
                class_name: ''
            },
            school: [],
            tableData: [],
            tableLoading: false,
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            // 学校列表
            school_tree: [],
            DialogVisibleList: false,
            tableDataReord: [],
            pageOrder: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            multipleSelection: [],
            checkAllStatus: false,
            checkIndeterminate: false,
            authResult: {},
        };
    },
    computed: {},
    watch: {
        school(value) {
            this.filterData.province_id = value ? value[0] : '';
            this.filterData.city_id = value ? value[1] : '';
            this.filterData.school_id = value ? value[2] : '';
            this.getTableData(1);
        }
    },
    filters: {
        setMoney(val) {
            if (!val) {
                return '--';
            }
            return `${Number(val).toFixed(2)}元`;
        },
        setAimtype (val) {
            if (val == 1) {
                return '自营商品订单';
            } else if (val == 2) {
                return '自营店铺';
            } else if (val == 3) {
                return '非自营店铺';
            }
        },
        setStatus (val) {
            if (val == 0) {
                return '已付款';
            } else if (val == 1) {
                return '返利待入账';
            } else if (val == 2) {
                return '返利已入账';
            } else if (val == 3) {
                return '已退款';
            }
        }
    },
    mounted() {
        this.authResult = this.$route.meta.authResult;
        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '掌大精英'}, {
            title: '掌大精英数据',
            path: '/student/student-data'
        }, {title: '精英粉丝'}]);
        this.authResult = this.$route.meta.authResult;
        this.getParam();
        this.getTableData();
        this.getSchoolTree();
    },

    methods: {

        /**
         * 获取url参数
         */
        getParam() {
            let query = this.$route.query;

            if (query.phone !== undefined || query.true_name !== undefined || query.purchase !== undefined || query.agent_no !== undefined) {
                this.filterData.is_buy = query.purchase;
                this.filterData.agent_no = query.agent_no;
                this.user = {
                    true_name: query.true_name,
                    phone: query.phone
                };
            }

            if (query.isFans) {
                this.isFans = false;
            }
        },

        /**
         * @description 获取省/市/学校三级联动
         */
        getSchoolTree() {
            this.$post('/student/local_info%5Cschool_tree', {}, resp => {
                if (resp.code === 1) {
                    this.school_tree = resp.data.tree;
                }
            });
        },

        /**
         * @description 获取列表数据
         * @param page
         */
        getTableData(page) {
            this.tableLoading = true;
            let params = {...this.filterData};

            params.page_id = page || this.page.now_page;
            this.$post('/student/stuagent_fans%5Cget_fans_list', params, res => {
                if (res.code === 1) {
                    this.tableData = res.data.list;
                    this.page = res.data.page;
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
                this.tableLoading = false;
            });
        },

        /**
         * 路由跳转
         * @param routeName
         * @param query
         */
        handleRoute(routeName, query) {
            this.$router.push({
                name: routeName,
                query
            });
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 进入下级粉丝页面
         */
        fnJumpFans(row) {
            if (row.fans_num == 0) {
                this.$message({
                    type: 'waring',
                    message: '暂时还没有二级粉丝',
                    center: true
                });
                return;
            }

            if (row.agent_no == '') {
                this.$message({
                    type: 'waring',
                    message: '该用户还未成为管培生',
                    center: true
                });
                return;
            }
            this.$router.replace({
                name: 'studentPeifanSub',
                query: {
                    phone: row.phone,
                    true_name: row.true_name,
                    agent_no: row.agent_no
                }
            });
        },

        /**
         * 选中
         * @param selection
         */
        handleSelectedChange(selection) {
            let checkedCount = selection.length;

            this.multipleSelection = selection;
            this.checkAllStatus = checkedCount === this.tableData.length;
            this.checkIndeterminate = checkedCount > 0 && checkedCount < this.tableData.length;
        },

        /**
         * 全选
         * @param val
         */
        handleSelectedAll(val) {
            if (val) {
                this.$refs.table.toggleAllSelection();
            } else {
                this.$refs.table.clearSelection();
            }
            this.checkIndeterminate = false;
        },

        /**
         * 删除
         */
        handleDelete() {
            let student_nos = this.multipleSelection.map(item => {
                return item.student_no;
            }).join();

            this.$confirm('此操作将批量删除选中粉丝, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/student/stuagent%5Cdelete_fans', {
                    student_no: student_nos
                }, resp => {
                    if (resp.code === 1) {
                        this.$notify({
                            type: 'success',
                            message: '删除成功',
                            title: '提示'
                        });
                        this.getTableData();
                    } else {
                        this.$notify({
                            type: 'waring',
                            message: resp.msg,
                            title: '提示'
                        });
                    }
                });
            });
        },

        /**
         * 查看购买记录
         */
        fnGetbuyList (row, page) {
            //查看操作执行
            if(row.student_no !== undefined) {
                this.student_no = row.student_no;
                this.DialogVisibleList = !this.DialogVisibleList;
            }

            this.$post('/student/stuagent_fans%5Cget_buy_list',{
                student_no: this.student_no,
                page_id: page || this.pageOrder.now_page
            },resp=>{
                if (resp.code == 1) {
                    this.tableDataReord = resp.data.list;
                    this.pageOrder.now_page = resp.data.page.now_page;
                    this.pageOrder.total_count = resp.data.page.total_count;
                    this.pageOrder.page_size = resp.data.page.page_size;
                    // this.tableDataReord = this.tableDataReord.concat(resp.data.list);
                    // this.DialogVisibleList = !this.DialogVisibleList;
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * 跳转订单列表 1:自营商品订单,2:淘宝客商品订单(自营店铺),3:淘宝客商品订单(非自营店铺)
         */
        fnJumpShop (row) {
            let data = {};

            if(row.aim_type == 1) {
                data = {
                    aim_no: row.aim_no,
                    fans: 0,
                    shop_type: 0
                };
            } else {
                data = {
                    aim_no: row.aim_no,
                    fans: 0,
                    shop_type: 2
                };
            }

            // this.$router.replace({
            //     name: 'OrderList',
            //     query: {
            //         ...data
            //     }
            // });

            let routeData = this.$router.resolve({
                name: 'OrderList',
                query: {
                    ...data
                }
            });

            window.open(routeData.href, '_blank');
        },
    }
};
</script>

<style lang="less" scoped>
.wrapper {
    width: 100%;

    .content-wrapper {
        display: flex;
        height: calc(100% - 24px);
        flex-direction: column;
        position: relative;
        margin: 12px;
        background: #fff;
        box-sizing: border-box;

        /*header*/

        .header {
            height: 68px;
            padding: 24px;
            box-sizing: border-box;
            box-shadow: 0px -1px 0px 0px #F0F2F5;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid #F0F2F5;

            .header-title {
                color: #333333;
                font-size: 20px;
                height: 20px;
                font-weight: 500;
                position: relative;
                margin: 24px 0 24px 12px;

                &::before {
                    content: '';
                    position: absolute;
                    left: -8px;
                    top: -1px;
                    width: 3px;
                    height: 20px;
                    background: #1890FF;
                }
            }
        }

        /*筛选*/

        .filter-view {
            box-sizing: border-box;
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            padding: 0 24px;
            margin-top: 12px;

            .header-new-box {
                display: flex;
                align-items: center;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #333333;
                white-space: nowrap;
                margin-right: 24px;
                margin-bottom: 12px;

                &-title {
                    margin-right: 16px;
                    white-space: nowrap;
                }
            }
        }

        // 表格
        .table-wrapper {
            padding: 0 24px;
            flex: 1;
            height: 0;

            .cell-title {
                font-family: PingFangSC-Medium, PingFang SC;
                font-weight: 500;
                color: #333333;
                margin-bottom: 12px;
                display: flex;
                align-items: center;

                .user-name {
                    max-width: 124px;
                    white-space: nowrap;
                    overflow: hidden;
                    text-overflow: ellipsis;
                }

                .user-tags {
                    display: flex;
                    align-items: center;
                    margin-left: 6px;

                    .tag-item {
                        margin-right: 4px;
                        display: flex;
                        align-items: center;

                        img {
                            height: 14px;
                        }
                    }
                }
            }

            .cell-content {
                display: -webkit-box;
                -webkit-box-orient: vertical;
                overflow: hidden;
                -webkit-line-clamp: 3;
            }

            .cell-tips {
                color: #999;
            }
        }

        .status-wrapper {
            display: flex;
            align-items: center;
            justify-content: center;

            .status-dot {
                width: 6px;
                height: 6px;
                border-radius: 50%;
                margin-right: 5px;

                &.success {
                    background: #4CD964;
                }

                &.danger {
                    background: #FF3030;
                }

                &.warning {
                    background: #FFA72D;
                }

                &.normal {
                    background: #A6A6A6;
                }
            }
        }

        /* 分页 */

        .pagination-wrapper {
            padding: 12px 24px;

            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }

    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow: 0 1px 0 0 rgba(233, 233, 233, 1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;

        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }

        .el-icon-close:before {
            color: rgb(194, 194, 194);
            font-size: 17px;
            font-weight: bold;
        }

        .el-dialog__headerbtn {
            top: 14px;
        }
    }

    /deep/ .el-textarea__inner {
        resize: none;
    }

    /deep/ .el-table__fixed-right {
        right: 6px !important;
    }

    .data-table {
        ::-webkit-scrollbar {
            width: 6px;
            height: 6px;
        }

        // 操作按钮
        .option-btn-group {
            .el-button {
                margin-left: 0;
                margin-right: 10px;
            }
        }
    }

    .zd-tips {
        color: #F8A127;
        font-size: 14px;
        margin-bottom: 24px;
    }

    .zd-content {
        display: flex;

        .zd-content-title {
            color: #000;
            font-size: 14px;
            white-space: nowrap;
            margin-right: 6px;
        }
    }

    .zd-foot {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-top: 30px;
        margin-bottom: 25px;
    }

    .zd-dialog-foot {
        width: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-top: 20px;
    }
}
</style>

<style lang="less">
.student-data-fans-view {
    padding: 12px 24px;
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #333333;

    &-item {
        margin: 8px 0;

        .value {
            color: #F79038;
        }
    }
}
</style>
